package com.ridanlabs.professorwebsite.general;

import org.springframework.security.core.GrantedAuthority;

public enum Authority implements GrantedAuthority {
    ADMIN (0),
    NEW_USER (1),
    USER (2),
    STUDENT (3),
    PROFESSOR (4);

    private final int bit;

    /**
     * Creates an authority with a specific bit representation. It's important that this doesn't
     * change as it will be used in the database. The enum ordinal is less reliable as the enum may be
     * reordered or have new roles inserted which would change the ordinal values.
     *
     * @param bit the permission bit which will represent this authority in the datastore.
     */
    Authority(int bit) {
        this.bit = bit;
    }

    public int getBit() {
        return bit;
    }

    public String getAuthority() {
        return toString();
    }
    
    /**
     * Compares Authority Constants as string with input. Ignores case 
     * when comparing.
     * @param authName
     * @return Null if authName is null or nothing is found, otherwise the Authority Enum representation.
     */
    public static Authority fromString( String authName ) {
    	if( authName == null ) return null;
    	
    	for( Authority a : Authority.values() ) {
    		if( a.getAuthority().equalsIgnoreCase(authName) )
    			return a;
    	}
    	
    	return null;
    }
}
